/*
 * EBFV1_hyperbolic-2.cpp
 *
 *  Created on: 15/09/2012
 *      Author: rogsoares
 */

#include "EBFV1_hyperbolic.h"

namespace PRS{

	EBFV1_hyperbolic::EBFV1_hyperbolic(){}
	EBFV1_hyperbolic::EBFV1_hyperbolic(PhysicPropData* _pPPData, SimulatorParameters* _pSimPar, MeshData* _pMData, OilProductionManagement* _pOP, TMesh* _pTMesh_data){
		pSimPar = _pSimPar;
		pPPData = _pPPData;
		pOPManager = _pOP;
		pHOApproximation = new HighOrderApproximation(pSimPar);
	}

	EBFV1_hyperbolic::~EBFV1_hyperbolic(){
		delete pHOApproximation;
	}

	double EBFV1_hyperbolic::solver(TMesh* pTMesh_data, double &timeStep){
		double startt = MPI_Wtime();
		PetscPrintf(PETSC_COMM_WORLD,"Hyperbolic solver...\t");
		this->resetTimeStep();

		// For High-order approximations
		if ( pSimPar->useHOApproximation() ){
			calculateSaturationGradient(pTMesh_data);
			pHOApproximation->getNodeSL_Ptr()->defineSlopeLimiters();
		}

		int dim = pTMesh_data->getMeshDim();
		int ndom = pTMesh_data->getNumDomains();
		for (int dom=0; dom<ndom; dom++){
			calculateVelocityField(dom,dim,pTMesh_data);
			resetNodalNonviscTerms(dom,pTMesh_data);
			calculateIntegralAdvectiveTerm(dom,dim,pTMesh_data);
		}
		//pMData->unifyScalarsOnMeshNodes(PhysicPropData::getNonViscTerm,PhysicPropData::setNonViscTerm,pGCData,0);
		timeStep = getTimeStep();														// take minimum time-step calculated by domain
		pSimPar->correctTimeStep(timeStep);												// correct time-step value to print out the desired simulation moment
		pSimPar->setAccumulatedSimulationTime(timeStep);								// AccSimTime = AccSimTime + timeStep
		saturationTimeAdvance(pTMesh_data,timeStep);									// Calculate saturation field: Sw(n+1)
		pSimPar->printOutVTK(pTMesh_data,exportSolutionToVTK);							// Output data (VTK)

		// The following lines below will be condensed to a function member call and it will belong to EBFV1_hyperbolic
		if (pSimPar->rankHasProductionWell()){
			pOPManager->printOilProduction(timeStep,pSimPar->getAccumulatedSimulationTime(),pSimPar->getSimTime());
		}
		PetscPrintf(PETSC_COMM_WORLD,"done.\n");
		double endt = MPI_Wtime();
		return endt-startt;
	}
}
